package core.weapons;
import graphics.SparkParticle;

import java.awt.*;

import core.Player;
import core.units.Unit;


/**
 * 
 * @author Anton
 *
 */
public class Mortar extends Attack {
	
	
	//protected final int FACTOR=10;
	int tx, ty, height = 0;
	int airtime=300, explodeTime=50, explodeRadius=100000;
	int timeElapsed=0, explodeFrame=0;

    public Mortar(int x1, int y1, int x2, int y2, Player m) {
    	super(x1, y1, m);
    	
    	damage=60;
    	lifetime = airtime+explodeTime;
    	x=x;
    	y=y;
    	tx=x2;
    	ty=y2;
    	/*attackQueue.add("wait");
    	attackQueue.add("dirChoice");
    	attackQueue.add("posChoice:400");
    	attackQueue.add("mortar");*/
    }
    /**
     * Currently broken. Hits too much.
     */
    public void checkHit(Unit u){ 
		if(explodeFrame==explodeTime-15 && u.getTeam()!=team){
			double dx=u.getX()-tx;
			double dy=u.getY()-ty;
			int range=(int)Math.sqrt(dx*dx+dy*dy);
			if(range<=explodeRadius)
				u.takeDamage(damage);
			hasHit=true;
			//System.out.print("i");
		}			
	}
	public void nextFrame(){
		height=height+timeElapsed-airtime/2;
			
		if(timeElapsed<airtime)
			timeElapsed++;
		else{
			explodeFrame++;
			
			for(int i=0; i<50; i++){
				//main.particles.add(new SparkParticle(tx, ty, 0, 0));//(int)(Math.cos(dir*3.1415*2)*speed), (int)(Math.sin(dir*3.1415*2)*speed)));
		
				main.particles.add(new SparkParticle(tx+(int)((Math.random()-0.5)*explodeRadius), ty+(int)((Math.random()-0.5)*explodeRadius), (int)(Math.random()*10000-5000), (int)(Math.random()*10000-5000)));
			
			}
		}
				
		lifetime--;
		if(lifetime==0)
			main.attacks.remove(this);
		//x=x+(int)(Math.cos(dir*3.1415*2)*speed);
		//y=y+(int)(Math.sin(dir*3.1415*2)*speed);
	}

	public void paint(Point screenPos, int zoomDivider, Graphics g){
		int drawX, drawY, drawRadius;
		int projectileSize=5000;
		if(timeElapsed<airtime){		
			g.setColor(Color.gray);
			drawX = (x-screenPos.x+(tx-x)*timeElapsed/airtime)/zoomDivider;
			drawY = (y-screenPos.y+(ty-y)*timeElapsed/airtime + height*50)/zoomDivider;
			drawRadius=projectileSize/zoomDivider;
    		g.fillOval(drawX-drawRadius, drawY-drawRadius, 2*drawRadius, 2*drawRadius);
		}
		else{
			g.setColor(Color.red);
			drawX=(tx-screenPos.x)/zoomDivider;
			drawY=(ty-screenPos.y)/zoomDivider;
			if(explodeFrame<=explodeTime-15)
				drawRadius=explodeRadius*(explodeFrame)/(explodeTime-15)/zoomDivider;				
			else
				drawRadius=explodeRadius*(explodeTime-explodeFrame)/(15)/zoomDivider;	

			g.drawOval(drawX-drawRadius, drawY-drawRadius, 2*drawRadius, 2*drawRadius);	
			
		}
	}
}
